package edu.jd.mainframe;

import edu.jd.log.LogDto;
import edu.jd.log.LogService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class AdminDialog extends JDialog {
    private LogService logService = new LogService();

    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    public AdminDialog(){}

    public AdminDialog(JFrame owner){
        super(owner, "查看日志", true);
        showing();
    }

    private void showing() {
        this.setSize(800, 400);
        JPanel root = new JPanel();
        this.setContentPane(root);
        root.setLayout(new BorderLayout());

        JScrollPane scPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        table.setRowSelectionAllowed(true);
        table.setRowHeight(30);
        root.add(scPane, BorderLayout.CENTER);

        model.addColumn("日志ID");
        model.addColumn("用户ID");
        model.addColumn("用户角色");
        model.addColumn("权限名称");
        model.addColumn("是否通过");
        model.addColumn("日期时间");

        List<LogDto> logList = logService.getLogs();
        for(LogDto dto : logList){
            addTableRow(dto);
        }

        this.setVisible(true);
    }

    public void addTableRow(LogDto item){
        Object rowData[] = new Object[6];
        rowData[0] = item.getLog_id();
        rowData[1] = item.getUser_id();
        rowData[2] = item.getRole_name();
        rowData[3] = item.getAuth_name();
        if(item.isState()){
            rowData[4] = "是";
        }else{
            rowData[4] = "否";
        }
        rowData[5] = item.getTime();
        model.addRow(rowData);
    }
}
